var utility    = require('system/utility'),
    App        = require('system/app'),
    HttpServer = require('system/httpserver'),
    cluster    = require('cluster'),
    http       = require('http');

if (cluster.isMaster) {
	numCPUs = require('os').cpus().length;

	cluster.on('exit', function(worker, code, signal) {
		console.log('worker ' + worker.process.pid + ' died ', code, signal);
	});

	utility.do(
		function() {
			var worker;

			for (var i = 0; i < 1; i++) {
				worker = cluster.fork();
			}
		},
		function() {
			console.log('All processes Started');
		}
	);
} else {
	var app = new App(
		{
			dataAccess : {
				mongo : {
					cadence : {
						address   : '',
						port      : 1,
						database  : 'cadence',
						isDefault : true
					}
				},
				sqlServer : {
				}
			},
			controllers : [
				require('assetmanagement/productsController'),
				require('assetmanagement/inventoryController'),
				require('contactInformation/entitiesController'),
				require('dataAccess/mongoController'),
				require('dataAccess/sqlServerController'),
				require('system/auditableController'),
				require('security/sessionsController'),
				require('security/securedObjectsController'),
				require('security/rolesController'),
				require('security/usersController'),
				require('geography/locationsController')
			],
			routes : [
				{ name : 'dataAccess/storage/save', handler : 'dataAccess.mongo.save', data : { _restricted : true } },
				{ name : 'dataAccess/storage/find', handler : 'dataAccess.mongo.find', data : { _restricted : true } },
				{ name : 'dataAccess/storage/eval', handler : 'dataAccess.mongo.eval', data : { _restricted : true } },
				{ name : 'dataAccess/storage/beginTransaction', handler : 'dataAccess.mongo.beginTransaction', data : { _restricted : true } },
				{ name : 'dataAccess/storage/commitTransaction', handler : 'dataAccess.mongo.commitTransaction', data : { _restricted : true } },
				{ name : 'dataAccess/storage/rollbackTransaction', handler : 'dataAccess.mongo.rollbackTransaction', data : { _restricted : true } },

				{ name : 'dataAccess/sqlServer/save', handler : 'dataAccess.sqlServer.save', data : { _restricted : true } },
				{ name : 'dataAccess/sqlServer/find', handler : 'dataAccess.sqlServer.find', data : { _restricted : true } },
				{ name : 'dataAccess/sqlServer/eval', handler : 'dataAccess.sqlServer.eval', data : { _restricted : true } },
				{ name : 'dataAccess/sqlServer/beginTransaction', handler : 'dataAccess.sqlServer.beginTransaction', data : { _restricted : true } },
				{ name : 'dataAccess/sqlServer/commitTransaction', handler : 'dataAccess.sqlServer.commitTransaction', data : { _restricted : true } },
				{ name : 'dataAccess/sqlServer/rollbackTransaction', handler : 'dataAccess.sqlServer.rollbackTransaction', data : { _restricted : true } },

				{ name : 'assetManagement/assets/save', handler : 'assetManagement.assets.save' },
				{ name : 'assetManagement/assets/find', handler : 'assetManagement.assets.find' },
				{ name : 'assetManagement/assets/findOne', handler : 'assetManagement.assets.findOne' },
				{ name : 'assetManagement/assets/exists', handler : 'assetManagement.assets.exists' },
				{ name : 'assetManagement/assets/search', handler : 'assetManagement.assets.search' },

				{ name : 'assetManagement/inventory/save', handler : 'assetManagement.inventory.save' },
				{ name : 'assetManagement/inventory/find', handler : 'assetManagement.inventory.find' },
				{ name : 'assetManagement/inventory/findOne', handler : 'assetManagement.inventory.findOne' },
				{ name : 'assetManagement/inventory/exists', handler : 'assetManagement.inventory.exists' },
				{ name : 'assetManagement/inventory/search', handler : 'assetManagement.inventory.search' },

				{ name : 'assetManagement/products/save', handler : 'assetManagement.products.save' },
				{ name : 'assetManagement/products/find', handler : 'assetManagement.products.find' },
				{ name : 'assetManagement/products/findOne', handler : 'assetManagement.products.findOne' },
				{ name : 'assetManagement/products/exists', handler : 'assetManagement.products.exists' },
				{ name : 'assetManagement/products/search', handler : 'assetManagement.products.search' },
				{ name : 'assetManagement/products/synchronize', handler : 'assetManagement.products.synchronize' },

				{ name : 'contactInformation/entities/save', handler : 'contactInformation.entities.save' },
				{ name : 'contactInformation/entities/find', handler : 'contactInformation.entities.find' },
				{ name : 'contactInformation/entities/findOne', handler : 'contactInformation.entities.findOne' },
				{ name : 'contactInformation/entities/exists', handler : 'contactInformation.entities.exists' },
				{ name : 'contactInformation/entities/search', handler : 'contactInformation.entities.search' },

				{ name : 'geography/locations/save', handler : 'geography.locations.save' },
				{ name : 'geography/locations/find', handler : 'geography.locations.find' },
				{ name : 'geography/locations/findOne', handler : 'geography.locations.findOne' },
				{ name : 'geography/locations/exists', handler : 'geography.locations.exists' },
				{ name : 'geography/locations/search', handler : 'geography.locations.search' },

				{ name : 'security/users/save', handler : 'security.users.save' },
				{ name : 'security/users/find', handler : 'security.users.find' },
				{ name : 'security/users/findOne', handler : 'security.users.findOne' },
				{ name : 'security/users/exists', handler : 'security.users.exists' },
				{ name : 'security/users/search', handler : 'security.users.search' },
				{ name : 'security/users/changePassword', handler : 'security.users.changePassword' },
				{ name : 'security/users/signIn', handler : 'security.users.signIn' },
				{ name : 'security/users/signOut', handler : 'security.users.signOut' },

				{ name : 'security/roles/save', handler : 'security.roles.save' },
				{ name : 'security/roles/find', handler : 'security.roles.find' },
				{ name : 'security/roles/findOne', handler : 'security.roles.findOne' },
				{ name : 'security/roles/exists', handler : 'security.roles.exists' },
				{ name : 'security/roles/search', handler : 'security.roles.search' },
				{ name : 'security/roles/getPermissions', handler : 'security.roles.getPermissions' },

				{ name : 'security/securedObjects/find', handler : 'security.securedObjects.find' },
				{ name : 'security/securedObjects/findOne', handler : 'security.securedObjects.findOne' },
				{ name : 'security/securedObjects/exists', handler : 'security.securedObjects.exists' },

				{ name : 'security/sessions/getSession', handler : 'security.sessions.getSession' },
				{ name : 'security/sessions/setSessionVariable', handler : 'security.sessions.setSessionVariable', data : { _restricted : true } },
				{ name : 'security/sessions/find', handler : 'security.sessions.find' },
				{ name : 'security/sessions/findOne', handler : 'security.sessions.findOne' },
				{ name : 'security/sessions/exists', handler : 'security.sessions.exists' }
			]
		}
	);

	var server = new HttpServer({
		app: app,
		port: parseInt(process.argv[2] || '8000'),
		address: '127.0.0.1',
		//rootPath: undefined, // no root -- fail instead
		rootPath: process.cwd().replace(/\\/g,'/') + "/files",
		defaultPage: "/index.html",
		failOverServer: {
			host: 'localhost',
			port: 1600
		},
		virtualPaths: [
			{
				name: '/$extjs/',
				folder: 'C:/Users/gskiba/Documents/Visual Studio/Cadence/Development/GTech.Cadence.Web/Client/lib/ExtJs-4.0.7-gpl/'
			}
		]
	});

	app.router.execute('security/users/signIn',
		{
			_requestId : 1,
			_sessionId : '1234',
			userName   : 'gkimmy',
			password   : 'gkimmy'
		},
		function(result) {

			if (!result.success) {
				console.log('result ', result);
				return;
			}

			app.router.execute('assetManagement/products/synchronize',
				{
					_requestId : 1,
					_sessionId : '1234',
					item : {
						userName : 'gkimmy'
					}
				},
				function(result) {
					console.log('result ', result);
				}
			);
		}
	);
}
